libname  "G:\data\workdata\707562"; 



DATA firm;
set dario.firm;
run;


DATA firm;
set firm;
rename aar=year;
run;


DATA firm;
set firm;
year2=year+0;
drop year;
rename year2=year;
run;

DATA industry_1;
set firm;
keep cvrnr year;
run;




DATA industry_2;
set dario.industry_95_98;
keep cvrnr year;
run;

data dario.industry;
set industry_1 industry_2;
run;


proc sort data=dario.industry;
by cvrnr year;
run;




DATA uhdi;
set dario.uhdi;
run;

DATA uhdi;
set uhdi;
keep land vare aar senr cvrnr vrd ie;
run;

/*ie 2 means that we look at export values, ie 1 is in fact import*/

DATA uhdi;
set uhdi;
if ie="2";
year2=aar+0;
drop aar;
rename year2=year;
vare2=vare+0;
drop vare;
rename vare2=vare;
HS4_exp=int(vare/100);
rename vrd=vrd_exp;
run;


DATA uhdi;
set uhdi;
if year>=1995;
run;



proc sort data=industry;
by cvrnr year;
run;


proc sort data=uhdi;
by cvrnr year;
run;

data export_ind_yes_merge;
merge industry (in=a) uhdi (in=b);
by cvrnr year;
if a and b;
run;



data export_ind_no_merge;
merge industry (in=a) uhdi (in=b);
by cvrnr year;
if b;
run;


DATA export_ind_no_merge;
set export_ind_no_merge;
rename cvrnr=cvrnr_old;
run;

proc sort data=export_ind_no_merge;
by senr;
run;

DATA senr_cvrnr;
set dario.senr_cvrnr;
run;


proc sort data=senr_cvrnr;
by senr;
run;


data export_ind_no_merge_2;
merge export_ind_no_merge (in=a) senr_cvrnr (in=b);
by senr;
if a and b;
run;


proc sort data=export_ind_no_merge_2;
by cvrnr year;
run;


data export_ind_no_merge_3;
merge export_ind_no_merge_2 (in=a) industry (in=b);
by cvrnr year;
if a and b;
run;


data dario.export_ind;
set export_ind_yes_merge export_ind_no_merge_3;
run;


proc sort data=dario.export_ind;
by cvrnr year;
run;

/*ie 2 means that we look at export values, ie 1 is in fact import*/


DATA uhdi;
set dario.uhdi;
run;

DATA uhdi;
set uhdi;
keep land vare aar senr cvrnr vrd ie;
run;

DATA uhdi;
set uhdi;
if ie="1";
year2=aar+0;
drop aar;
rename year2=year;
vare2=vare+0;
drop vare;
rename vare2=vare;
HS4_imp=int(vare/100);
rename vrd=vrd_imp;
run;


DATA uhdi;
set uhdi;
if year>=1995;
run;

proc sort data=industry;
by cvrnr year;
run;


proc sort data=uhdi;
by cvrnr year;
run;

data import_ind_yes_merge;
merge industry (in=a) uhdi (in=b);
by cvrnr year;
if a and b;
run;



data import_ind_no_merge;
merge industry (in=a) uhdi (in=b);
by cvrnr year;
if b;
run;


DATA import_ind_no_merge;
set import_ind_no_merge;
rename cvrnr=cvrnr_old;
run;

proc sort data=import_ind_no_merge;
by senr;
run;

DATA senr_cvrnr;
set dario.senr_cvrnr;
run;


proc sort data=senr_cvrnr;
by senr;
run;


data import_ind_no_merge_2;
merge import_ind_no_merge (in=a) senr_cvrnr (in=b);
by senr;
if a and b;
run;


proc sort data=import_ind_no_merge_2;
by cvrnr year;
run;


data import_ind_no_merge_3;
merge import_ind_no_merge_2 (in=a) industry (in=b);
by cvrnr year;
if a and b;
run;


data import_ind;
set import_ind_yes_merge import_ind_no_merge_3;
run;


proc sort data=import_ind;
by cvrnr year;
run;

DATA vars;
set dario.vars;
run;

DATA vars;
set vars;
keep aar varenr  cvrnr;
run;

DATA vars;
set vars;
aar2=substr(aar,1,4);
year2=aar2+0;
drop aar aar2;
rename year2=year;
HS4_dom_1=int(varenr/10000);
run;



proc sort data=vars;
by cvrnr year;
run;




DATA vark;
set dario.vark;
run;

DATA vark;
set vark;
keep aar varenr cvrnr;
run;

DATA vark;
set vark;
year2=aar+0;
drop aar;
rename year2=year;
HS4_dom_2=int(varenr);
run;



proc sort data=vark;
by cvrnr year;
run;



data import_ind_a;
merge import_ind (in=a) dario.export_ind (in=b);
by cvrnr year;
if a;
run;



data import_ind_b;
merge import_ind_a (in=a) vars (in=b);
by cvrnr year;
if a or b;
run;


data dario.import_ind_c;
merge import_ind_b (in=a) vark (in=b);
by cvrnr year;
if a or b;
run;
